package org.agridata.akvisualization.service.impl;

import org.agridata.akvisualization.doMain.vo.OriginVo;
import org.agridata.akvisualization.mapper.QuarantineStatisticsMapper;
import org.agridata.akvisualization.service.QuarantineStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class QuarantineStatisticsServiceImpl implements QuarantineStatisticsService {

    @Autowired
    private QuarantineStatisticsMapper mapper;

    @Autowired
    private DiseaseSurveillanceServiceImpl dis;

    @Override
    public Map<String, Object> getOriginStatisticsList(Integer times) {

        HashMap<String, Object> map = new HashMap<>();

        List<OriginVo> originList = mapper.getOriginStatisticsListByTime(times);
        List<OriginVo> slaughterList = mapper.getSlaughterStatisticsListByTime(times);
        List<OriginVo> originLists = new ArrayList<>();
        List<OriginVo>originResult = zhengHeOrigin(originList);
        Map<String, List<OriginVo>> stringListMap = zhengHeSlaughter(slaughterList, originLists);

        map.put("origin",originResult);
        map.put("slaughter",stringListMap);

        return map;
    }

    private Map<String, List<OriginVo>> zhengHeSlaughter(List<OriginVo> slaughterList, List<OriginVo> originLists) {

        for (OriginVo originVo : slaughterList) {
            if (originVo.getAnimalID() == 12||originVo.getAnimalID()==16||originVo.getAnimalID()==11||originVo.getAnimalID()==38||originVo.getAnimalID()==13
                    ||originVo.getAnimalID()==99913||originVo.getAnimalID()==30000||originVo.getAnimalID()==70000||originVo.getAnimalID()==4||originVo.getAnimalID()==43||originVo.getAnimalID()==40000) {
                originVo.setAnimalName("其它");
                originVo.setAnimalID(0);
            }
            if (originVo.getAnimalID() == 8 || originVo.getAnimalID() == 7 || originVo.getAnimalID() == 6 || originVo.getAnimalID() == 20400 || originVo.getAnimalID() == 9 || originVo.getAnimalID() == 29900||originVo.getAnimalID() == 10) {
                originVo.setAnimalName("禽");
                originVo.setAnimalID(20000);
            }
            originLists.add(originVo);
        }
        List<OriginVo> result = originLists.stream()
                .collect(Collectors.toMap(
                        vo -> vo.getMonth() + "-" + vo.getAnimalID(), // 以月份和AnimalID作为Map的key
                        Function.identity(), // 使用元素本身作为Map的值
                        (o1, o2) -> {
                            // 如果o1或o2为null，则返回另一个对象，否则相加它们的counts
                            if (o1 == null) return o2;
                            if (o2 == null) return o1;
                            o1.setCounts(o1.getCounts() + o2.getCounts());
                            return o1;
                        }
                ))
                .values()
                .stream()
                .collect(Collectors.toList());

        System.out.println("-=-=-=-=-="+result);

        Map<String, List<OriginVo>> listMap = new HashMap<String, List<OriginVo>>();
        //对数据进行春防、秋防分类
        for (OriginVo originVo : result) {
            String pinyin = dis.toPinyin(originVo.getAnimalName());
            //验证key是否存在
            if (listMap.containsKey(pinyin)) {
                //如果存在直接添加对应的key中的list中
                listMap.get(pinyin).add(originVo);
            } else {
                //不存在创建一个新的list并put进map中
                ArrayList<OriginVo> arrayList = new ArrayList<>();
                arrayList.add(originVo);
                listMap.put(pinyin, arrayList);
            }
        }

        listMap.forEach((key, value) -> {
            // 对每个列表按照月份进行排序
            value.sort(Comparator.comparing(OriginVo::getMonth));
        });

        return listMap;
    }

    public List<OriginVo> zhengHeOrigin(List<OriginVo> originList) {
        List<OriginVo> originLists = new ArrayList<>();
        for (OriginVo originVo : originList) {
            if (originVo.getAnimalID() == 0||originVo.getAnimalID()==16||originVo.getAnimalID()==10000||originVo.getAnimalID()==38||originVo.getAnimalID()==13
                    ||originVo.getAnimalID()==99913||originVo.getAnimalID()==30000||originVo.getAnimalID()==70000||originVo.getAnimalID()==4||originVo.getAnimalID()==43||originVo.getAnimalID()==40000){
                originVo.setAnimalName("其它");
                originVo.setAnimalID(0);
            }
            if (originVo.getAnimalID() == 8 || originVo.getAnimalID() == 7 || originVo.getAnimalID() == 6 || originVo.getAnimalID() == 20400 || originVo.getAnimalID() == 9 || originVo.getAnimalID() == 29900) {
                originVo.setAnimalName("禽");
                originVo.setAnimalID(20000);
            }
            originLists.add(originVo);
        }
        List<OriginVo> result = originLists.stream()
                // 表示id为key， 接着如果有重复的，那么从BillsNums对象o1与o2中筛选出一个，这里选择o1，
                // 并把id重复，需要将nums和sums与o1进行合并的o2, 赋值给o1，最后返回o1
                .collect(Collectors.toMap(OriginVo::getAnimalID, a -> a, (o1, o2)-> {
                    o1.setCounts(o1.getCounts() + o2.getCounts());
                    return o1;
                })).values().stream().collect(Collectors.toList());
        System.out.println("-=-=-=-=-="+result);
        return result;
    }
}
